package online.flowerinsnow.minecraft.bukkit.manhunt.listener;

import online.flowerinsnow.minecraft.bukkit.manhunt.event.PlayerEatSkullEvent;
import online.flowerinsnow.minecraft.bukkit.manhunt.manager.RecipeManager;
import online.flowerinsnow.minecraft.bukkit.manhunt.session.item.ItemStackSession;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;
import online.flowerinsnow.minecraft.bukkit.manhunt.core.PlayerTeam;
import online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils;

public class SkullEatListener implements Listener {
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
        if (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
        ItemStack item = e.getItem();
        if (item == null) return;
        if (item.getType() == Material.PLAYER_HEAD) {
            ItemStackSession session = new ItemStackSession(item);
            if (session.getNBTTagCompoundBoolean("eatable")) {
                e.setCancelled(true);
                String iteam = session.getNBTTagCompoundString("team");
                PlayerTeam pteam = Manhunt.getTeamManager().getPlayerTeam(e.getPlayer());
                if (pteam != null) {
                    if (pteam.name().equals(iteam)) {
                        e.getPlayer().sendMessage(MessageUtils.parseColor("&7[&c×&7]&c你怎么忍心吃了自己队友的头呢?"));
                        return;
                    }
                    PlayerEatSkullEvent event = new PlayerEatSkullEvent(e.getPlayer(), e.getItem(), false);
                    Bukkit.getPluginManager().callEvent(event);
                    if (event.isCancelled()) return;
                    if (costInteractItem(e)) {
                        giveHeadBuff(e.getPlayer());
                    }
                }
            }
        } else if (item.getType() == Material.GOLDEN_APPLE) {
            ItemMeta meta = item.getItemMeta();
            if (meta != null && meta.hasEnchant(Enchantment.ARROW_INFINITE) && item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) {
                e.setCancelled(true);
                PlayerEatSkullEvent event = new PlayerEatSkullEvent(e.getPlayer(), e.getItem(), true);
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) return;
                if (costInteractItem(e)) {
                    giveGHeadBuff(e.getPlayer());
                }
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onCraft(CraftItemEvent e) {
        HumanEntity clicker = e.getWhoClicked();
        if (!(clicker instanceof Player)) return;
        Player player = (Player) e.getWhoClicked();
        if (e.getRecipe().getResult().equals(RecipeManager.GHEAD)) {
            ItemStack center = e.getInventory().getMatrix()[4];
            if (center == null) return;
            ItemStackSession session = new ItemStackSession(center);
            if (!session.getNBTTagCompoundBoolean("eatable")) {
                e.setCancelled(true);
                player.sendMessage(MessageUtils.parseColor("&7[&c×&7]&c这个头不能制作成金头"));
                return;
            }
            String iteam = session.getNBTTagCompoundString("team");
            PlayerTeam pteam = Manhunt.getTeamManager().getPlayerTeam(player);
            if (pteam != null) {
                if (pteam.name().equals(iteam)) {
                    e.setCancelled(true);
                    player.sendMessage(MessageUtils.parseColor("&7[&c×&7]&c你怎么忍心把队友的头给做成金头呢?"));
                }
            }
        }
    }

    private void giveHeadBuff(Player player) {
        player.removePotionEffect(PotionEffectType.ABSORPTION);
        player.removePotionEffect(PotionEffectType.REGENERATION);
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 150, 1));
        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 2400, 1));
        player.setAbsorptionAmount(6.0);
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP, 1.0F, 1.0F);
    }

    private void giveGHeadBuff(Player player) {
        player.removePotionEffect(PotionEffectType.ABSORPTION);
        player.removePotionEffect(PotionEffectType.REGENERATION);
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 200, 2));
        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 2400, 2));
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP, 1.0F, 1.0F);
    }

    private boolean costInteractItem(PlayerInteractEvent e) {
        ItemStack item = e.getItem();
        if (item == null) return false;
        if (item.getAmount() < 2) {
            if (e.getHand() == EquipmentSlot.HAND) {
                e.getPlayer().getInventory().setItemInMainHand(null);
                return true;
            } else if (e.getHand() == EquipmentSlot.OFF_HAND) {
                e.getPlayer().getInventory().setItemInOffHand(null);
                return true;
            }
        } else {
            item.setAmount(item.getAmount() - 1);
            return true;
        }
        return false;
    }
}
